package senpec.qc.harmony.instrument.equipment.calibration;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import senpec.qc.harmony.base.exception.NoResultException;
import senpec.qc.harmony.base.mybatisplus.IBaseServiceImpl;
import senpec.qc.harmony.base.mybatisplus.Result;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@RequiredArgsConstructor
public class CalibrationServiceImpl extends IBaseServiceImpl<CalibrationMapper, Calibration> implements CalibrationService {
    private final CalibrationMapper calibrationMapper;

    @Override
    public Result<Calibration> getById(Long id) {
        Calibration calibration = calibrationMapper.selectById(id);
        if (calibration == null) {
            throw new NoResultException("未找到该数据");
        } else {
            return Result.ok(calibration);
        }
    }

    @Override
    public Result<List<Calibration>> getListByConditions(Calibration conditions) {
        List<Calibration> calibrationList = calibrationMapper.selectList(null);
        return Result.ok(calibrationList);
    }


    @Override
    public Result<List<Calibration>> getListByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<Calibration> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.equals(columnName, "equipmentId")) {
            queryWrapper.eq(Calibration::getEquipmentId, columnValue);
        }
        queryWrapper.orderByDesc(Calibration::getStartDate);
        List<Calibration> calibrationList = calibrationMapper.selectList(queryWrapper);
        return Result.ok(calibrationList);
    }

    @Override
    public Result<Calibration> insertOne(Calibration entity) {
        calibrationMapper.insert(entity);
        return Result.ok(entity);
    }

}